package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.ChooseAddressBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * ----编辑地址
 */
public class CompileAddressActivity extends BaseActivity implements View.OnClickListener {
    private final int FROM_WHEEL_CODE = 642;
    private TextView title;
    private EditText et_name;
    private EditText et_phone;
    private TextView tv_city;
    private EditText tv_area;
    private EditText et_doorplate;

    private String name;
    private String phone;
    private String city;
    private String area;
    private String doorplate;
    private int index1;

    private String inputName;
    private String inputPhone;
    private String inputCity;
    private String inputArea;
    private String inputDoorplate;

    private String province;
    private String citys;
    private String district;
    private String addressId;

    private String sheng;
    private String shi;
    private String qu;
    private ChooseAddressBean addressBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compile_address);

        initData();
        initView();
    }

    private void initData() {
        addressBean = (ChooseAddressBean) getIntent().getSerializableExtra("ChooseAddressBean");
        name = getIntent().getStringExtra("name");
        phone = getIntent().getStringExtra("phone");
        city = getIntent().getStringExtra("city");
        area = getIntent().getStringExtra("area");
        doorplate = getIntent().getStringExtra("doorplate");
        index1 = getIntent().getIntExtra("index", 0);
        addressId = getIntent().getStringExtra("addressId");
        sheng = getIntent().getStringExtra("sheng");
        shi = getIntent().getStringExtra("shi");
        qu = getIntent().getStringExtra("qu");
        System.out.println(index1 + "======index1");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("编辑收货地址");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_save).setOnClickListener(this);
        findViewById(R.id.tv_delete).setOnClickListener(this);
        findViewById(R.id.rl_city).setOnClickListener(this);

        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        tv_city = (TextView) findViewById(R.id.tv_city);
        tv_area = (EditText) findViewById(R.id.tv_area);
        et_doorplate = (EditText) findViewById(R.id.et_doorplate);

        et_name.setText(name);
        et_phone.setText(phone);
        tv_city.setText(city);
        tv_area.setText(addressBean.getAddress());
        et_doorplate.setText(doorplate);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        Integer index = (Integer) v.getTag();
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //城市
            case R.id.rl_city:
                intent = new Intent(this, WheelActivity.class);
                startActivityForResult(intent, FROM_WHEEL_CODE);
                break;

            //保存
            case R.id.tv_save:
                inputName = et_name.getText().toString();
                inputPhone = et_phone.getText().toString();
                inputCity = tv_city.getText().toString();
                inputArea = tv_area.getText().toString();
                inputDoorplate = et_doorplate.getText().toString();
                if (inputName == null || "".equals(inputName)) {
                    MyToast("请输入收件人姓名");
                } else if (!MyUtils.isMobileNO(inputPhone)) {
                    MyToast("请输入正确手机号");
                } else if (inputCity == null || "".equals(inputCity)) {
                    MyToast("请选择城市");
                } else if (inputArea == null || "".equals(inputArea)) {
                    MyToast("请输入详细地址");
                } else {
                    uploadByxUtils();
                }


                break;

            //删除
            case R.id.tv_delete:
                Map<String, Object> map = new HashMap<>();
                map.put("userId", MyApplication.getInstance().userBean.getUserId());
                map.put("addressId", addressId);
                showDialog();
                XUtil.Post(ConfigUtil.DELADDRESS, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        System.out.println(result + "===delete");

                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            String res = jsonObject.optString("result");
                            String info = jsonObject.optString("info");

                            if ("1".equals(res)) {
                                finish();
                                MyToast(info);
                            } else if ("2".equals(res)) {
                                MyToast("删除失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                    }

                    @Override
                    public void onFinished() {
                        super.onFinished();
                        closeDialog();
                    }
                });
                break;

            default:
                break;
        }
    }

    private void uploadByxUtils() {
        String addr = inputArea + inputDoorplate;
        System.out.println(inputName + "add===1");
        System.out.println(addr + "add===2");
        System.out.println(province + "add===3");
        System.out.println(citys + "add===4");
        System.out.println(district + "add===5");
        System.out.println(inputPhone + "add===6");
        System.out.println(addressId + "add===7");
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("name", inputName);
        map.put("address", addr);
        if (province == null) {
            map.put("province", sheng);
        } else {
            map.put("province", province);
        }
        if (citys == null) {
            map.put("city", shi);
        } else {
            map.put("city", citys);
        }
        if (district == null) {
            map.put("area", qu);
        } else {
            map.put("area", district);
        }
        map.put("phoneNumber", inputPhone);
        map.put("addressId", addressId);
        XUtil.Post(ConfigUtil.EDITADDRESS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                System.out.println(result + "编辑add===");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                       /* //数据是使用Intent返回
                       Intent intent = new Intent();

                        //把返回数据存入Intent
                        intent.putExtra("name", inputName);
                        intent.putExtra("phone", inputPhone);
                        intent.putExtra("city", inputCity);
                        intent.putExtra("area", inputArea);
                        intent.putExtra("doorplate", inputDoorplate);
                        //设置返回数据
                        CompileAddressActivity.this.setResult(RESULT_OK, intent);

                        //关闭Activity*/
                        MyToast(info);
                        finish();
                    } else if ("2".equals(res)) {
                        MyToast("修改失败");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FROM_WHEEL_CODE:
                if (resultCode == RESULT_OK) {
                    province = data.getExtras().getString("province");
                    citys = data.getExtras().getString("city");
                    district = data.getExtras().getString("district");

                    tv_city.setText(province + citys + district);
                }

                break;

            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
